<form class="layui-form" action="{:url('', ['material_sku' => $materialSku, 'client_ordernum' => $clientOrdernum, 'material_return_id' => $materialReturnId, 'feed_id' => $feedId, '_t'=>$_t])}" method="post" id="editForm" lay-filter="edit-form">
    
    {:FormExt::input('物料名称', 'name', arrayGet($materialInfo, 'name', ''), '请输入物料名称', 'required', 'lay-verify="required"')}
    {:FormExt::input('物料SKU', 'material_sku', arrayGet($materialInfo, 'material_sku', ''), '请输入物料SKU', 'required', 'lay-verify="required"')}
    {:FormExt::input('订单号', 'client_ordernum', $clientOrdernum, '请输入订单号', '', '')}
    {:FormExt::select('工厂', 'factory_id', arrayGet($orderInfo, 'factory_id', 0), $factoryKV, '', '')}
    {:FormExt::input('入库数量', 'stock', '', '请输入入库数量', '', 'lay-verify="number"')}
    {:FormExt::select('单位', 'purchasing_unit', '', $unitKV, '', '')}
    {:FormExt::input('单价', 'unit_price', '', '请输入单价', '', 'lay-verify="number"')}
    {:FormExt::input('总金额', 'amount', '', '请输入总金额', '', 'lay-verify="number"')}
    <div class="pop-bottom-bar">
    	<input type="hidden" class="field-id" name="id">
        <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="formSubmit" hisi-data="{pop: true, refresh: false, callback: callbackUpdateStock}">提交保存</button>
        <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
    </div>
</form>

{include file="system@block/layui" /}
    
<script>
layui.use(['tool', 'form', 'layer'], function() {
	var $ = layui.jquery;
	var form = layui.form;
	var layer = layui.layer;
	
	$('input[name=stock]').focus();
	
	window.calAmount = function(){
		var d = form.val("edit-form");
		var stock = 0;
		var unitPrice = 0;
		var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
		
		if (d.stock != '') {
			if (!reg.test(d.stock)) {
				layer.msg('数量格式错误', {icon: 2,anim: 6});
				return false;
			}
			stock = layui.tool.parseFloat(d.stock, 2);
		}
		if (d.unit_price != '') {
			if (!reg.test(d.unit_price)) {
				layer.msg('单价格式错误', {icon: 2,anim: 6});
				return false;
			}
			unitPrice = layui.tool.parseFloat(d.unit_price, 2);
		}
		
		$('input[name=amount]').val(layui.tool.bcmul(stock, unitPrice));
	};
	
	$(document).on('blur','input[name=stock],input[name=unit_price]',function(){
		calAmount();
	});
	
	// 表单提交回调
	window.callbackUpdateStock = function(obj, res) {
		if (res.code == 1) layer.msg(res.msg, {icon: 1,end:function(){parent.layui.layer.closeAll();}});
		var stock = 0;
		var mid = {$mid};
		if (mid > 0){
			// 更新采购进度库存数
			if (typeof(eval(parent.window.updateStock))==='function'){
				parent.window.updateStock(mid, res.data.stock);
			}
		}
		
		// 更新补料任务状态
		if (typeof(eval(parent.window.updateDistributingFeedStatus))==='function'){
			parent.window.updateDistributingFeedStatus(res.data.feed_id);
		}
		
		// 更新退料进度状态
		if (typeof(eval(parent.window.updateMaterialReturnStatus))==='function'){
			parent.window.updateMaterialReturnStatus(res.data.material_return_id);
		}
	}
});
</script>